package com.jasperframework.boot.file.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;


/**
 * <p>  </p>
 *
 * @author daizhao
 * @site https://miliqkdoc.motopa.cn/
 * @date 2022/12/12 12:12
 */
@Configuration
public class SpringMVCConfig implements WebMvcConfigurer{

    @Value(value = "${fms.baseDir}")
    private String basePath;

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String path = "file:" + basePath + "fmsf/";
        registry.addResourceHandler("/fmsf/**").addResourceLocations(path);
    }

    /**
     * 文件上传临时路径
     * 在Spring Boot下配置location，可以在main()方法所在文件中添加如下代码：
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setLocation(basePath + "/temp");
        return factory.createMultipartConfig();
    }
}
